package com.have.blog.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.PictureDto;
import com.have.blog.admin.service.AdminPictureService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.BlogMapper;
import com.have.blog.model.mapper.PictureCategoryMapper;
import com.have.blog.model.mapper.PictureMapper;
import com.have.blog.model.models.*;
import com.have.blog.picture.service.PictureFeignApi;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class AdminPictureServiceImpl implements AdminPictureService {
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private PictureCategoryMapper pictureCategoryMapper;
    @Autowired
    private PictureFeignApi pictureFeignApi;

    @Override
    public List<PictureDto> getList(PictureDto pictureDto) {
        PageHelper.startPage(pictureDto.getPageNum(), pictureDto.getPageSize());
        PictureExample example = new PictureExample();
        PictureExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(pictureDto.getKeyword()) && !StringUtils.isEmpty(pictureDto.getKeyword().trim())) {
            criteria.andPicNameLike("%" + pictureDto.getKeyword() + "%");
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andPictureCategoryUidEqualTo(pictureDto.getPictureCategoryUid());
        example.setOrderByClause("create_time desc");
        List<Picture> pictures = pictureMapper.selectByExample(example);
        List<PictureDto> resultList = MyBeanUtils.copyListProperties(pictures, PictureDto::new);
        if (CollectionUtils.isEmpty(pictures)) {
            return resultList;
        }
        List<Integer> picUids = new ArrayList<>();
        for (PictureDto dto : resultList) {
            picUids.add(dto.getUid());
        }
        Wrapper<Map<Integer, String>> wrapper = pictureFeignApi.getPicturesUrl(picUids);
        if (wrapper.isSuccess()) {
            Map<Integer, String> data = wrapper.getData();
            for (PictureDto dto : resultList) {
                dto.setPictureUrl(data.get(dto.getUid()));
            }
        }
        return resultList;
    }

    @Transactional
    @Override
    public Wrapper addPicture(List<PictureDto> pictureDtoList) {
        if(!CollectionUtils.isEmpty(pictureDtoList)) {
            // TODO 修改批量插入，事务效率太低
            for (PictureDto item : pictureDtoList) {
                Picture picture = new Picture();
                MyBeanUtils.copyProperties(item, picture);
                picture.setStatus(BlogStatus.ENABLE.getStatus());
                pictureMapper.updateByPrimaryKeySelective(picture);
            }
        } else {
            return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
        }
        return Wrapper.successWithMessage(AdminMessageConstant.INSERT_SUCCESS);
    }

    @Transactional
    @Override
    public Wrapper editPicture(PictureDto pictureDto) {
        Picture picture = pictureMapper.selectByPrimaryKey(pictureDto.getUid());
        if (picture == null) {
            return Wrapper.failed(AdminMessageConstant.THE_PICTURE_NOT_EXIST);
        }
        picture.setStatus(BlogStatus.DISABLE.getStatus());
        pictureMapper.updateByPrimaryKeySelective(picture);
        Picture update = new Picture();
        update.setUid(pictureDto.getNewUid());
        update.setPictureCategoryUid(picture.getPictureCategoryUid());
        int result = pictureMapper.updateByPrimaryKeySelective(update);
        // TODO 发送消息到RabbitMq
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper deleteBatchPicture(List<Integer> uids) {
        if (CollectionUtils.isEmpty(uids)) {
            return Wrapper.illegal();
        }
        PictureExample example = new PictureExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andUidIn(uids);
        Picture template = new Picture();
        template.setStatus(BlogStatus.DISABLE.getStatus());
        int delete = pictureMapper.updateByExampleSelective(template, example);
        if (delete > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
    }

    @Override
    public Wrapper setPictureCover(PictureDto pictureDto) {
        PictureCategory pictureCategory = pictureCategoryMapper.selectByPrimaryKey(pictureDto.getPictureCategoryUid());
        if (pictureCategory != null) {
            Picture picture = pictureMapper.selectByPrimaryKey(pictureDto.getUid());
            if (picture != null) {
                pictureCategory.setCoverPictureUid(picture.getUid());
                pictureCategoryMapper.updateByPrimaryKeySelective(pictureCategory);
            } else {
                return Wrapper.failed(AdminMessageConstant.THE_PICTURE_NOT_EXIST);
            }
        } else {
            return Wrapper.failed(AdminMessageConstant.THE_PICTURE_SORT_NOT_EXIST);
        }
        return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
    }
}
